import time

from selenium.webdriver import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.expected_conditions import visibility_of_element_located
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

from common.Utils import Driver, QuestDriver


class QuestLogin:
    url = ""
    driver = ""
    def __init__(self):
        self.url = "http://8.155.1.153:8081/user/login"
        self.driver = QuestDriver.driver
        self.driver.get(self.url)

    #成功登录测试
    def LoginSucTest(self):
        wait = WebDriverWait(self.driver, 10)
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys(Keys.CONTROL, 'a')
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys(Keys.DELETE)
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys(Keys.CONTROL, 'a')
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys(Keys.DELETE)

        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys("1011")
        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR,"#root > div > div.content___2zk1- > div.main___x4OjT > div > form > button").click()
        #判断是否登录成功
        # time.sleep(1)
        wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR,"#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-page-header.ant-pro-page-container-warp-page-header.ant-pro-page-container-warp-page-header.ant-page-header-has-breadcrumb.ant-page-header-ghost > div.ant-page-header-content > div > div > div > div > div > div.content___1eLtZ > div.contentTitle___Yu-cR")))
        #添加屏幕截图
        QuestDriver.getScreenShot()
        self.driver.back()

    #错误的用户名和密码
    def LoginFailTest1(self):
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys(Keys.CONTROL, 'a')
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys(Keys.DELETE)
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys(Keys.CONTROL, 'a')
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys(Keys.DELETE)

        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys("987")
        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("1234567")
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#root > div > div.content___2zk1- > div.main___x4OjT > div > form > button").click()

        # time.sleep(1)
        text = self.driver.find_element(By.CSS_SELECTOR,"#root > div > div.content___2zk1- > div.main___x4OjT > div > form > div.ant-alert.ant-alert-error > div > div").text
        QuestDriver.getScreenShot()
        assert text == "错误的用户名和密码"
        self.driver.refresh()


    #错误的用户名和正确的密码
    def LoginFailTest2(self):
        #这个项目clear清理不了，可以键盘全选之后在输入，或者全选后删除在输入
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys(Keys.CONTROL, 'a')
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("987")

        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys(Keys.CONTROL, 'a')
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys(Keys.DELETE)
        # time.sleep(3)

        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#root > div > div.content___2zk1- > div.main___x4OjT > div > form > button").click()

        # time.sleep(1)
        text = self.driver.find_element(By.CSS_SELECTOR,"#root > div > div.content___2zk1- > div.main___x4OjT > div > form > div.ant-alert.ant-alert-error > div > div").text
        QuestDriver.getScreenShot()
        assert text == "错误的用户名和密码"
        self.driver.refresh()


    #正确的用户名和错误的密码
    def LoginFailTest3(self):
        #先删除再输入
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys(Keys.CONTROL, 'a')
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys(Keys.DELETE)
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys(Keys.CONTROL, 'a')
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys(Keys.DELETE)

        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys("1011")
        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("1234567")
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#root > div > div.content___2zk1- > div.main___x4OjT > div > form > button").click()

        # time.sleep(1)
        text = self.driver.find_element(By.CSS_SELECTOR,"#root > div > div.content___2zk1- > div.main___x4OjT > div > form > div.ant-alert.ant-alert-error > div > div").text
        QuestDriver.getScreenShot()
        assert text == "错误的用户名和密码"
        self.driver.refresh()

    #跳转到注册页面
    def LoginToRegister(self):
        time.sleep(2)
        self.driver.find_element(By.CSS_SELECTOR,"#root > div > div.content___2zk1- > div.main___x4OjT > div > form > div:nth-child(4) > a").click()

        #判断是否调转成功
        text=self.driver.find_element(By.CSS_SELECTOR,"#root > div > div.content___1k5Ro > div.main___19HXK > div > form > button > span").text
        QuestDriver.getScreenShot()
        assert text == "注册账户"
        self.driver.back()


# login = QuestLogin()
# login.LoginSucTest()
# login.LoginFailTest1()
# login.LoginFailTest2()
# login.LoginFailTest3()
# login.LoginToRegister()
# QuestDriver.driver.quit()